/**
 */
package com.dingpeikeji.quickdeer.core.modules.site.web;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.core.modules.site.service.TSitedtlService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Area;



/**
 * 站点Controller
 * @author lh
 * @version 2016-05-19
 */
@Controller
@RequestMapping(value = "${adminPath}/site/tSitedtl")
public class TSitedtlController extends BaseController {

	@Autowired
	private TSitedtlService tSitedtlService;

	@ModelAttribute
	public TSitedtl get(@RequestParam(required=false) String id) {
		TSitedtl entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tSitedtlService.get(id);
		}
		if (entity == null){
			entity = new TSitedtl();
		}
		return entity;
	}
	
	@RequiresPermissions("site:tSitedtl:view")
	@RequestMapping(value = {"list", ""})
	public String list(@ModelAttribute("siteQry") TSitedtl siteQry, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TSitedtl> page = tSitedtlService.findPage(new Page<TSitedtl>(request, response), siteQry); 
		model.addAttribute("page", page);
		return "modules/site/tSitedtlList";
	}

	@RequiresPermissions("site:tSitedtl:view")
	@RequestMapping(value = "form")
	public String form(TSitedtl tSitedtl, Model model) {
		model.addAttribute("tSitedtl", tSitedtl);
		return "modules/site/tSitedtlForm";
	}

	@RequiresPermissions("site:tSitedtl:edit")
	@RequestMapping(value = "save")
	public String save(TSitedtl tSitedtl, Model model, RedirectAttributes redirectAttributes) {
		
		if (!beanValidator(model, tSitedtl)){
			return form(tSitedtl, model);
		}
		//修改站点名称
		if (tSitedtlService.getTSiteBySitename
				(tSitedtl.getSitename(), tSitedtl.getSpottype(),tSitedtl.getCityname()) != null) {
			addMessage(model, "站点已存在，请确认后再提交！");
			return form(tSitedtl, model);
		}
		
		tSitedtlService.save(tSitedtl);
		addMessage(model, "修改成功！");
		return "redirect:"+Global.getAdminPath()+"/site/tSitedtl/?repage";
	}
	
	@RequiresPermissions("site:tSitedtl:edit")
	@RequestMapping(value = "delete")
	public String delete(TSitedtl tSitedtl, RedirectAttributes redirectAttributes) {
		tSitedtlService.delete(tSitedtl);
		addMessage(redirectAttributes, "删除站点成功");
		return "redirect:"+Global.getAdminPath()+"/site/tSitedtl/?repage";
	}
	
	@RequiresPermissions("site:tSitedtl:edit")
	@RequestMapping(value = "setHot")
	public String setHot(String qryname, String qrycityid, String qrycityname, TSitedtl tSitedtl,RedirectAttributes redirectAttributes) throws UnsupportedEncodingException {
		tSitedtlService.update(tSitedtl);
		addMessage(redirectAttributes, "设置热点成功");
		
		String transname = URLDecoder.decode(qryname, "UTF-8");
		String transcity = URLDecoder.decode(qrycityname, "UTF-8");
		TSitedtl siteQry = new TSitedtl();
		siteQry.setSitename(transname);
		siteQry.setArea(new Area(qrycityid, transcity));
		redirectAttributes.addFlashAttribute("siteQry", siteQry);  
		return "redirect:"+Global.getAdminPath()+"/site/tSitedtl/?repage";
	}

	/**
	 * 验证站点名称是否唯一
	 * @param oldSitename
	 * @param siteanme
	 * @return
	 */
	@ResponseBody
	@RequiresPermissions("site:tSitedtl:edit")
	@RequestMapping(value = "checkSitename")
	public String checkSitename(String oldSitename, String spottype, String sitename,String cityname) {
		if (sitename !=null && sitename.equals(oldSitename)) {
			return "true";
		} else if (sitename !=null && tSitedtlService.getTSiteBySitename(sitename, spottype,cityname) == null) {
			return "true";
		}
		return "false";
	}
	
}
